// pages/addressmg/addressmg.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollleft: 0,
    modify: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getConsigneeInfo()

  },
  randomWord() {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    var nums = "";
    for (var i = 0; i < 20; i++) {
      var id = parseInt(Math.random() * 61);
      nums += chars[id]
    }
    return nums;
  },
  getConsigneeInfo() {
    var t = this
    wx.request({
      url: app.globalData.Interfaceurl + 'getConsigneeInfo',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
      },
      success: res => {
        // console.log(res)
        t.setData({
          consigneeInfoList: res.data.consigneeInfoList
        })
        if (t.data.consigneeInfoList.length == 0) {

          wx.showModal({
            title: '提示',
            content: '请添加收货地址',
            showCancel: true,
            cancelColor: "#000000",
            cancelText: '取消',
            success(res) {
              if (res.confirm) {
                wx.chooseAddress({
                  success: (res) => {
                    // console.log(res)
                    t.addConsigneeInfo(res)

                  },
                  fail: function (err) {
                    console.log(err)
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }

      }
    })



  },
  addConsigneeInfo(res) {

    // if (condition) {

    // }
    var t = this
    wx.request({
      url: app.globalData.Interfaceurl + 'addConsigneeInfo',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        consigneeName: res.userName,
        consigneeMobile: res.telNumber,
        consigneeProvince: res.provinceName,
        consigneeCity: res.cityName,
        consigneeArea: res.countyName,
        consigneeAddress: res.detailInfo,
        defaultFlag: 1,
        token: t.data.token,
      },
      success: res => {
        // console.log(res)
        t.getConsigneeInfo()

      }
    })
  },

  //跟新地址
  updataadd(e) {
    // data-consigneeInfoId="{{item.id}}" 
    // data-consigneeName="{{item.consigneeName}}" 
    // data-="{{item.consigneeMobile}}"
    // data-="{{item.consigneeProvince}}"
    // data-="{{item.consigneeCity}}"
    // data-="{{item.consigneeArea}}"
    // data-="{{item.consigneeAddress}}"
    // data-="{{item.defaultFlag}}"
    var dataup = {
      'consigneeInfoId': e.currentTarget.dataset.consigneeinfoid,
      'consigneeName': e.currentTarget.dataset.consigneename,
      'consigneeMobile': e.currentTarget.dataset.consigneemobile,
      'consigneeProvince': e.currentTarget.dataset.consigneeprovince,
      'consigneeCity': e.currentTarget.dataset.consigneecity,
      'consigneeArea': e.currentTarget.dataset.consigneearea,
      'consigneeAddress': e.currentTarget.dataset.consigneeaddress,
      'defaultFlag': e.currentTarget.dataset.defaultflag,
    }

    wx.navigateTo({
      url: '/pages/addressupdate/addressupdate?updatas=' + JSON.stringify(dataup),
    })
  },
  //删除地址
  deleadd(e) {
    var t = this
    // console.log(e)

    wx.showModal({
      title: '提示',
      content: '您将删除该收获地址',
      success(res) {
        if (res.confirm) {

          wx.request({
            url: app.globalData.Interfaceurl + 'delConsigneeInfo',
            method: "post",
            header: {
              "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
            },
            data: {
              userId: app.globalData.userId,
              consigneeInfoId: e.currentTarget.dataset.consigneeinfoid,
            },
            success: res => {
              // console.log(res)
              if (res.data.result == 0 && res.statusCode == 200) {
                t.onLoad()
                t.setData({
                  scrollleft: 0
                })
              } else {
                wx.showToast({
                  title: '操作失败，请重试',
                  icon: 'none'
                })
              }

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //收货地址调起
  chooseAddress() {
    var t = this
    if (t.data.consigneeInfoList.length > 5) {
      wx.showToast({
        title: '您只能添加6条收货信息',
        icon: "none"
      })
    } else {
      wx.chooseAddress({
        success: (res) => {
          // console.log(res)
          t.addConsigneeInfo(res)

        },
        fail: function (err) {
          console.log(err)
        }
      })
    }
  },
  // 选择地址
  choiceadd(e) {
    console.log(e)
    let pages = getCurrentPages(); //当前页面    （pages就是获取的当前页面的JS里面所有pages的信息）

    let prevPage = pages[pages.length - 2]; //上一页面（prevPage 就是获取的上一个页面的JS里面所有pages的信息）

    prevPage.setData({
      consigneeName: e.currentTarget.dataset.consigneename,
      consigneeMobile: e.currentTarget.dataset.consigneemobile,
      consigneeAddress: e.currentTarget.dataset.consigneeprovince + '' +
        e.currentTarget.dataset.consigneecity + "" + e.currentTarget.dataset.consigneearea + "" +
        e.currentTarget.dataset.consigneeaddress,
      trafficCost: e.currentTarget.dataset.trafficcost,
    })

    wx.navigateBack({ //返回

      delta: 1

    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.modify == 1) {
      this.getConsigneeInfo()
    }

    var sjs = this.randomWord()
    var datatime = Date.parse(new Date());
    var tokens = '' + sjs + '' + datatime + '' + app.globalData.userId
    this.setData({
      token: tokens,
      scrollleft: 0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})